#include "timer.h"
#include "LPC17xx.h"
#include "clock.h"
#include "interrupt.h"
#include "led.h"


/**
 * Timer2 initialization
 *
 * @param  none
 * @return none
 *
 * @brief  initialize hardware timer 2
 *
 *
 */
void timer2_init(void)
{
    /* power on periphery */
    SC->PCONP |=  TIMER2_POWERON;

    TIM2->PC = 0;

    /* set prescaler value */
    TIM2->PR = MAIN_FREQUENCY / TIMER_FRQ_VALUE;

    /* set speed to 2 us */
    TIM2->MR0 = 1;


    TIM2->CTCR = TIMER_MODE_PC_MATCH;

    /* enable interrupt, reset timer when interrupt occurs */
    TIM2->MCR = MR0_INTERRUPT_ENABLED | MR0_RESET_TC;

    /* enable timer */
    TIM2->TCR |= TIMER_ENABLE;

    /* set NVIC isr priority */
    NVIC_SetPriority(TIMER2_IRQn, TIM2_NVIC_PRIO);

    /* enable NVIC interrupt */
    NVIC_EnableIRQ(TIMER2_IRQn);
}

/**
 * Timer2 interrupt service routine
 *
 * @param  none
 * @return none
 *
 * @brief  handle isr of timer 2
 *
 *
 */
void timer2_isr(void)
{
    unsigned int status = 0;
    status = TIM2->IR;

    //TODO add action item here

    /* reset interrupt */
    TIM2->IR = status;
}

